import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ScreenCapture extends HttpServlet {

	private static final long serialVersionUID = 1L;

	protected void processRequest(HttpServletRequest req,
			HttpServletResponse res) throws ServletException, IOException {
		res.setContentType("text/html;charset=euc-kr");
		req.setCharacterEncoding("euc-kr");
		PrintWriter out = res.getWriter();

		String jsonData = "Screen Capture";

		out.println(jsonData);
		String browser = req.getParameter("browser");
		int top = Integer.parseInt(req.getParameter("top"));
		int left = Integer.parseInt(req.getParameter("left"));
		int width = Integer.parseInt(req.getParameter("width"));
		int height = Integer.parseInt(req.getParameter("height"));
		String dummy = req.getParameter("dummy");
		String fileName = dummy + width + height + req.getParameter("fileName");

		System.out.println(jsonData);
		System.out.println("browser : " + browser + ",   top : " + top
				+ ",   left : " + left + ",   width : " + width
				+ ",   height : " + height);

		capture(left, top, width, height, fileName);
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	public void insertDB() {
		HttpServletRequest request = null;
		
		int id;
		String userid;
		String name;
		int rows;
		int cols;
		int cellMarginTop;
		int cellMarginLeft;
		int cellWidth;
		int cellHeight;
		int borderWidth;

		String color_00;
		String borderColor_00;
		int borderWidth_00;
		int borderRound_00;
		String borderType_00;

		String color_01;
		String bordercolor_01;
		String bordertype_01;
		String borderround_01;

		String color_02;
		String bordercolor_02;
		String bordertype_02;
		String borderround_02;
		String flag;

		id = Integer.parseInt(request.getParameter("id"));
		userid = request.getParameter("userid");
		name = request.getParameter("name");
		rows = Integer.parseInt(request.getParameter("rows"));
		cols = Integer.parseInt(request.getParameter("cols"));
		cellMarginTop = Integer.parseInt(request.getParameter("cellmargintop"));
		cellMarginLeft = Integer.parseInt(request
				.getParameter("cellmarginleft"));
		cellWidth = Integer.parseInt(request.getParameter("cellwidth"));
		cellHeight = Integer.parseInt(request.getParameter("cellheight"));
		borderWidth = Integer.parseInt(request.getParameter("borderwidth"));
		color_00 = request.getParameter("color_00");
		borderColor_00 = request.getParameter("bordercolor_00");
		borderWidth_00 = Integer.parseInt(request
				.getParameter("borderwidth_00"));
		borderRound_00 = Integer.parseInt(request
				.getParameter("borderround_00"));
		borderType_00 = request.getParameter("bordertype_00");
		color_01 = request.getParameter("color_01");
		bordercolor_01 = request.getParameter("bordercolor_01");
		bordertype_01 = request.getParameter("bordertype_01");
		borderround_01 = request.getParameter("borderround_01");
		color_02 = request.getParameter("color_02");
		bordercolor_02 = request.getParameter("bordercolor_02");
		bordertype_02 = request.getParameter("bordertype_02");
		borderround_02 = request.getParameter("borderround_02");

	}

	public void capture(int x, int y, int width, int height, String fileName) {
		try {
			BufferedImage screencapture = null;
			try {
				screencapture = new Robot().createScreenCapture(new Rectangle(
						x, y, width, height));
				File file = new File(
						"D:\\_Dev\\workspace\\d2\\WebContent\\files\\"
								+ fileName);
				ImageIO.write(screencapture, "jpg", file);
				// JPG // ImageIO.write(screencapture, "png", file); // PNG
			} catch (Exception ex) {
				System.out.println(ex.getMessage());
			}
		} catch (Exception e) {
		}
	}
}